import BaseState from "./BaseState";

export class BaseRunState extends BaseState {


    private linearVelocity: cc.Vec2 = cc.v2();
    private directionVec2: cc.Vec2 = cc.v2();
    public EnterState(): void {
        super.EnterState();
    }

    public ExitState(): void {
        super.ExitState();
    }

    public UpdataState(dt: number): void {
        super.UpdataState(dt);
        if (!this.characterEle.pathArr || this.characterEle.pathArr.length == 0) {
            return;
        }

        if (Math.abs(this.characterEle.node.x - this.characterEle.pathArr[this.characterEle.pathIndex].x) > 20) {
            this.directionVec2.x = this.characterEle.pathArr[this.characterEle.pathIndex].x - this.characterEle.node.x;
            this.directionVec2.normalizeSelf();
            this.linearVelocity.x = this.directionVec2.x * this.characterEle.moveSpeed;
            this.characterEle.rb2d.linearVelocity = this.linearVelocity;
            this.characterEle.changeDirection(this.characterEle.pathArr[this.characterEle.pathIndex]);
        } else {
            this.characterEle.pathIndex++;
            if (this.characterEle.pathIndex >= this.characterEle.pathArr.length) {
                //到达终点回收对象
                this.characterEle.deadToDo();
            }
        }

    }

}
